package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.FictionSection;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 小说章节
 * TableName : FICTION_SECTION
 * ClassName : com.swlz.dao.FictionSectionDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-30 14:14:49
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class FictionSectionDAO {

	/**
	 * 添加一个小说章节
	 * 包含所有字段
	 * @param fictionSection - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(FictionSection fictionSection) throws SQLException {
		if(fictionSection.getSectionId()==null) {
			fictionSection.setSectionId(DBUtil.nextId());
		}
		return DBUtil.insert(fictionSection);
	}
 
	
	/**
	 * 添加一个小说章节
	 * 只包含非null的字段
	 * @param fictionSection - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(FictionSection fictionSection) throws SQLException {
		if(fictionSection.getSectionId()==null) {
			fictionSection.setSectionId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(fictionSection);
	}
	

	/**
	 * 根据主键更新一个小说章节
	 * 包含所有字段
	 * @param fictionSection - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(FictionSection fictionSection) throws SQLException {
		return DBUtil.update(fictionSection);
	}
	
	/**
	 * 根据主键更新一个小说章节
	 * 只包含非null的字段
	 * @param fictionSection - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(FictionSection fictionSection) throws SQLException {
		return DBUtil.updateSelective(fictionSection);
	}
	
	/**
	 * 根据主键删除一个小说章节
	 * @param sectionId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long sectionId) throws SQLException  {
		return DBUtil.execute("DELETE FROM FICTION_SECTION WHERE SECTION_ID=?", sectionId);
	}
	
	/**
	 * 根据主键获取一个小说章节
	 * @param sectionId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public FictionSection selectById(Long sectionId) throws SQLException  {
		return DBUtil.get("SELECT * FROM FICTION_SECTION WHERE SECTION_ID=?", FictionSection.class ,sectionId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param sectionId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public FictionSection selectByModel(FictionSection fictionSection)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION_SECTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionSection,sql,params);
		return DBUtil.get(sql.toString(), FictionSection.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param sectionId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<FictionSection> selectByModel(FictionSection fictionSection,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION_SECTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionSection,sql,params);
		sql.append(" ORDER BY SECTION_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), FictionSection.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param sectionId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(FictionSection fictionSection)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM FICTION_SECTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fictionSection,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param sectionId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(FictionSection fictionSection,StringBuffer sql,List<Object> params) {
		if(fictionSection==null){
			return;
		}

		if(fictionSection.getSectionId()!=null) {
			sql.append(" AND SECTION_ID=?");
			params.add(fictionSection.getSectionId());
		}

		if(fictionSection.getFictionId()!=null) {
			sql.append(" AND FICTION_ID=?");
			params.add(fictionSection.getFictionId());
		}

		if(fictionSection.getSectionTitle()!=null) {
			sql.append(" AND SECTION_TITLE=?");
			params.add(fictionSection.getSectionTitle());
		}

		if(fictionSection.getSectionContent()!=null) {
			sql.append(" AND SECTION_CONTENT=?");
			params.add(fictionSection.getSectionContent());
		}

		if(fictionSection.getSectionCreateTime()!=null) {
			sql.append(" AND SECTION_CREATE_TIME=?");
			params.add(fictionSection.getSectionCreateTime());
		}

		if(fictionSection.getSectionLastTime()!=null) {
			sql.append(" AND SECTION_LAST_TIME=?");
			params.add(fictionSection.getSectionLastTime());
		}

		if(fictionSection.getSectionLastMemo()!=null) {
			sql.append(" AND SECTION_LAST_MEMO=?");
			params.add(fictionSection.getSectionLastMemo());
		}

		if(fictionSection.getSectionTime()!=null) {
			sql.append(" AND SECTION_TIME=?");
			params.add(fictionSection.getSectionTime());
		}

		if(fictionSection.getSectionPassTime()!=null) {
			sql.append(" AND SECTION_PASS_TIME=?");
			params.add(fictionSection.getSectionPassTime());
		}

		if(fictionSection.getSectionPass()!=null) {
			sql.append(" AND SECTION_PASS=?");
			params.add(fictionSection.getSectionPass());
		}

		if(fictionSection.getSectionPassUserId()!=null) {
			sql.append(" AND SECTION_PASS_USER_ID=?");
			params.add(fictionSection.getSectionPassUserId());
		}

		if(fictionSection.getSectionPassMemo()!=null) {
			sql.append(" AND SECTION_PASS_MEMO=?");
			params.add(fictionSection.getSectionPassMemo());
		}

		if(fictionSection.getSectionRechargeable()!=null) {
			sql.append(" AND SECTION_RECHARGEABLE=?");
			params.add(fictionSection.getSectionRechargeable());
		}

	}
}